package com.gugee.ins.web.controller.view;

import com.gugee.ins.web.dto.FollowerSearch;
import com.gugee.ins.web.service.DicService;
import com.gugee.ins.web.service.InsRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class InsRankingController extends InsWebBaseController {

    @Autowired
    InsRankService insRankService;

    @Autowired
    DicService dicService;


    /**
     * 总排行榜分类国家
     * @param category
     * @param nation
     * @return
     */
    @RequestMapping(value = "/rank-{category}-influencers-{region}",method = RequestMethod.GET)
    public ModelAndView influencersRank(@PathVariable String category,@PathVariable String nation){
        ModelAndView modelAndView = getModel(21);
        modelAndView.setViewName("ins/rank/index");
        influencersRankModel(modelAndView,category,nation);
        return modelAndView;
    }

    /**
     * 总排行榜
     * @return
     */
    @RequestMapping(value = "/rank-influencers",method = RequestMethod.GET)
    public ModelAndView influencersRankAll(){
        ModelAndView modelAndView = getModel(21);
        modelAndView.setViewName("ins/rank/index");
        influencersRankModel(modelAndView,"","");
        return modelAndView;
    }

    private void influencersRankModel(ModelAndView modelAndView,String category,String nation){
        FollowerSearch followerSearch = new FollowerSearch();
        followerSearch.setCategoryName(category);

        String nationCode = nation;
        if(StringUtils.isEmpty(nation)){
            nationCode = dicService.getCode(nation);
        }
        followerSearch.setNation(dicService.getCode(nationCode));
        followerSearch.setDateCode(insRankService.getUpdateDateCodeByFollowerRank(category,nationCode));
        modelAndView.addObject("category",category);
        modelAndView.addObject("nation",nation);
        modelAndView.addObject("listData",insRankService.getFollowerRankPage(followerSearch));
        modelAndView.addObject("updateTime",insRankService.getUpdateTimeByFollowerRank(category,nationCode));
        modelAndView.addObject("categories",dicService.getOverallCategory());
        modelAndView.addObject("nations",dicService.getNations(1));
    }

    @RequestMapping(value = "/follower-growth",method = RequestMethod.GET)
    public ModelAndView followerGrowthRank(){
        ModelAndView modelAndView = getModel(22);
        modelAndView.setViewName("ins/follower/growth");
        return modelAndView;
    }
}
